﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;

public partial class Index : System.Web.UI.Page
{
    PM.PMSys.account aaccount = new PM.PMSys.account();
    PM.PMSys.news thenews = new PM.PMSys.news();
    PM.PMBasic.Mail amail = new PM.PMBasic.Mail();
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (Session["tid"] == null)
        {
            hideSession.Value="no";
        }
        else
        {
            ShowPanel();
            hideSession.Value = "yes";
        }
        if (!IsPostBack)
        {
            Repeaterdatabind();
            LoadDeclare();
            LoadJudge();
            LoadTop10();
        }
    }

    //显示用户信息
    protected void ShowPanel()
    {
        if (Session["sex"].ToString() == "女")
            Image1.ImageUrl = "~/Images/f" + Session["usertype"].ToString() + ".png";
        else Image1.ImageUrl = "~/Images/m" + Session["usertype"].ToString() + ".png";
        lblname.Text = Session["name"].ToString();
        lbldepart.Text = Session["depart"].ToString();
        switch (Session["usertype"].ToString())
        {
            case "1": lblUsertype.Text = "普通教师"; break;
            case "2": lblUsertype.Text = "学院管理员"; break;
            case "3": lblUsertype.Text = "项目管理员"; break;
            case "4": lblUsertype.Text = "系统管理员"; break;
            default: lblUsertype.Text = "未知权限"; break;
        }
        int a = amail.GetUnread(Session["tid"].ToString());
        if (a > 0)
        {
            lblmail.Text = a.ToString();
            lblmail.ForeColor = System.Drawing.Color.Red;
            imgmail.ImageUrl = "~/images/Mail New.png";
        }
    }
    //用户登录按钮
    protected void btnLogin_Click(object sender, ImageClickEventArgs e)
    {
        if (Request.Cookies["CheckCode"] == null)
       {
           divnote.InnerHtml = "<span style=\"color:red;\">您的浏览器已被禁用Cookies，请设置浏览器允许使用 Cookies 。</span>";
           return;
       }
       if (txtCode.Text.ToString().Trim().ToUpper().Equals(Request.Cookies["CheckCode"].Value))
       {
           int result = aaccount.UserLogin(txtName.Text.ToString().Trim(), txtPassword.Text.ToString().Trim());
           if (result == 1)
           {
               Response.Redirect("Main.aspx");
           }
           else if (result == 0)
           {
               divnote.InnerHtml = "<span style=\"color:red;\">您的账户未通过审核，请联系管理员审核您的账户。</span>";
           }
           else
               divnote.InnerHtml = "<span style=\"color:red;\">账号密码错误或者该账号不存在！</span>";
       }
       else
       { 
           divnote.InnerHtml = "<span style=\"color:red;\">验证码输入错误!</span>";
       }
            
    }
    //用户注册按钮
    protected void btnRegister_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Reg.aspx");
    }
    //进入系统按钮
    protected void btnjinru_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Main.aspx");
    }
    //注销按钮
    protected void btnzhuxiao_Click(object sender, ImageClickEventArgs e)
    {
        Session.RemoveAll();
        Response.Redirect("Index.aspx");
    }
    //载入repeater数据列表
    protected void Repeaterdatabind()
    {
        DataTable dt = new DataTable();
        dt = thenews.GetList(20, "1=1", "date desc");
        Repeater1.DataSource = dt;
        Repeater1.DataBind();

    }
    //载入已开启申报的项目
    protected void LoadDeclare()
    {
        StringBuilder sb = new StringBuilder();
        StringBuilder Sqlstr = new StringBuilder();
        sb.Append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">");
        Sqlstr.AppendFormat("select top 10 btnname,opendate,closedate from View_ProjectPlan where opendate<'{0}' and closedate>'{1}' order by type",DateTime.Now.ToString(),DateTime.Now.ToString()) ;
        DataTable dt = new DataTable();
        dt = PM.Code.DAL.DbHelper.ExecuteDataTableBySql(Sqlstr.ToString());
        if (dt.Rows.Count==0)
        {
            sb.Append("<tr>");
            sb.Append("<td>暂无任何项目信息.....</td>");
            sb.Append("</tr>");
        } 
        else
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sb.Append("<tr>");
                sb.Append("<td>" + dt.Rows[i][0].ToString() + "</td><td>" + (DateTime.Parse(dt.Rows[i][1].ToString())).ToShortDateString() + " 至 " + (DateTime.Parse(dt.Rows[i][2].ToString())).ToShortDateString() + "</td>");
                sb.Append("</tr>");
            }
        }
        sb.Append("</table>");
        divdeclare.InnerHtml=sb.ToString();
    }
    //载入已开启审批的项目
    protected void LoadJudge()
    {
        StringBuilder sb = new StringBuilder();
        StringBuilder Sqlstr = new StringBuilder();
        sb.Append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">");
        Sqlstr.AppendFormat("select top 10 btnname,opentime,closetime from View_ApprovePlan where opentime<'{0}' and closetime>'{1}' order by id", DateTime.Now.ToString(), DateTime.Now.ToString());
        DataTable dt = new DataTable();
        dt = PM.Code.DAL.DbHelper.ExecuteDataTableBySql(Sqlstr.ToString());
        if (dt.Rows.Count == 0)
        {
            sb.Append("<tr>");
            sb.Append("<td>暂无任何项目信息.....</td>");
            sb.Append("</tr>");
        }
        else
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sb.Append("<tr>");
                sb.Append("<td>" + dt.Rows[i][0].ToString() + "</td><td>" + (DateTime.Parse(dt.Rows[i][1].ToString())).ToShortDateString() + " 至 " + (DateTime.Parse(dt.Rows[i][2].ToString())).ToShortDateString() + "</td>");
                sb.Append("</tr>");
            }
        }
        sb.Append("</table>");
        divjudge.InnerHtml = sb.ToString();
    }

    protected void LoadTop10()
    {
        StringBuilder sb = new StringBuilder();
        StringBuilder strTable = new StringBuilder();
        sb.Append("select top 10 pname from Bus_Project where status='3' order by approvaldate desc");
        SqlDataReader reader = PM.Code.DAL.DbHelper.ExecuteDataReaderBySql(sb.ToString());
        strTable.Append("<ul>");
        while (reader.Read())
        {
            strTable.AppendFormat("<li>{0}</li>", reader[0].ToString());
        }
        strTable.Append("</ul>");
        if (reader.HasRows)
        {
             top10.InnerHtml = strTable.ToString();
        }
        else
        {
           top10.InnerHtml = "无最近立项的项目";
        }
        reader.Close();
    }

    //截取多余字符并高亮显示需要高亮的标题
    static public string strTrim(string str, string hightlight)
    {
        string tmpStr;
        tmpStr = PM.Code.Basic.pmbasic.strCal(str, 60);
        if (hightlight.Equals("1"))
        {
            tmpStr = "<span style=\"color:red;\">" + tmpStr + "</span>";
        }
        return tmpStr;
    }
    //将数据库中的长时间转变成短时间
    static public string ToShortTime(string time)
    {
        string date;
        date = (Convert.ToDateTime(time)).ToShortDateString();
        return date;
    }
}
